package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.AppOrder;
import org.dromara.system.mapper.AppOrderMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.AppQualityInfoBo;
import org.dromara.system.domain.vo.AppQualityInfoVo;
import org.dromara.system.domain.AppQualityInfo;
import org.dromara.system.mapper.AppQualityInfoMapper;
import org.dromara.system.service.IAppQualityInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 质检信息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppQualityInfoServiceImpl implements IAppQualityInfoService {

    private final AppQualityInfoMapper baseMapper;
    private final AppOrderMapper appOrderMapper;

    /**
     * 查询质检信息
     *
     * @param id 主键
     * @return 质检信息
     */
    @Override
    public AppQualityInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询质检信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 质检信息分页列表
     */
    @Override
    public TableDataInfo<AppQualityInfoVo> queryPageList(AppQualityInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppQualityInfo> lqw = buildQueryWrapper(bo);
        Page<AppQualityInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的质检信息列表
     *
     * @param bo 查询条件
     * @return 质检信息列表
     */
    @Override
    public List<AppQualityInfoVo> queryList(AppQualityInfoBo bo) {
        LambdaQueryWrapper<AppQualityInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppQualityInfo> buildQueryWrapper(AppQualityInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppQualityInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppQualityInfo::getId);
        lqw.eq(bo.getSampleWeight() != null, AppQualityInfo::getSampleWeight, bo.getSampleWeight());
        lqw.eq(bo.getPlasticContent() != null, AppQualityInfo::getPlasticContent, bo.getPlasticContent());
        lqw.eq(bo.getMoisture() != null, AppQualityInfo::getMoisture, bo.getMoisture());
        lqw.eq(bo.getAsh() != null, AppQualityInfo::getAsh, bo.getAsh());
        lqw.eq(bo.getCarbon() != null, AppQualityInfo::getCarbon, bo.getCarbon());
        lqw.eq(bo.getHydrogen() != null, AppQualityInfo::getHydrogen, bo.getHydrogen());
        lqw.eq(bo.getOxygen() != null, AppQualityInfo::getOxygen, bo.getOxygen());
        lqw.eq(bo.getNitrogen() != null, AppQualityInfo::getNitrogen, bo.getNitrogen());
        lqw.eq(bo.getSulfur() != null, AppQualityInfo::getSulfur, bo.getSulfur());
        lqw.eq(bo.getFluorine() != null, AppQualityInfo::getFluorine, bo.getFluorine());
        lqw.eq(bo.getBromine() != null, AppQualityInfo::getBromine, bo.getBromine());
        lqw.eq(StringUtils.isNotBlank(bo.getQualityEvaluation()), AppQualityInfo::getQualityEvaluation, bo.getQualityEvaluation());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialGrade()), AppQualityInfo::getMaterialGrade, bo.getMaterialGrade());
        lqw.eq(StringUtils.isNotBlank(bo.getVerificationNote()), AppQualityInfo::getVerificationNote, bo.getVerificationNote());
        lqw.eq(StringUtils.isNotBlank(bo.getInspector()), AppQualityInfo::getInspector, bo.getInspector());
        lqw.eq(bo.getInspectionDate() != null, AppQualityInfo::getInspectionDate, bo.getInspectionDate());
        return lqw;
    }

    /**
     * 新增质检信息
     *
     * @param bo 质检信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppQualityInfoBo bo) {
        AppQualityInfo add = MapstructUtils.convert(bo, AppQualityInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            //更新订单状态
            LambdaQueryWrapper<AppOrder> appOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            appOrderLambdaQueryWrapper.eq(AppOrder::getOrderId,bo.getOrderId());
            AppOrder appOrder = appOrderMapper.selectOne(appOrderLambdaQueryWrapper);
            if (appOrder != null) {
                appOrder.setQualityId(add.getId());
                appOrder.setOrderStatus(10L);
                appOrderMapper.updateById(appOrder);
            }
        }
        return flag;
    }

    /**
     * 修改质检信息
     *
     * @param bo 质检信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppQualityInfoBo bo) {
        AppQualityInfo update = MapstructUtils.convert(bo, AppQualityInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppQualityInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除质检信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
